﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using PPI.Entities.Inspections;
using VMind.Global;

namespace PPI.Client.Logic.Inspection
{
    public class LCompany
    {
        public static List<ECompany> SelectAll()
        {
            List<ECompany> listCompanies = new List<ECompany>();
            List<EInspector> listInspectors = new List<EInspector>();
            DataTable dtCompany = WebConsumer.Inspection.WCompany.SelectAll().Tables[0];
            DataTable dtInspectors = WebConsumer.Inspection.WInspector.SelectAll().Tables[0];

            foreach (DataRow drInspector in dtInspectors.Rows)
            {
                EInspector newInspector = new EInspector(ConvertHelper.ToInt32(drInspector["InspectorId"]), drInspector["Name"].ToString());
                newInspector.Company.Id = ConvertHelper.ToInt32(drInspector["CompanyFk"]);
                listInspectors.Add(newInspector);
            }
            foreach (DataRow drCompany in dtCompany.Rows)
            {
                ECompany newCompany = new ECompany(ConvertHelper.ToInt32(drCompany["UtilityId"]), drCompany["Name"].ToString());
                List<EInspector> tmpInspectors = listInspectors.FindAll(delegate(EInspector i) { return i.Company.Id == newCompany.Id; });
                if (tmpInspectors != null)
                    newCompany.Inspectors = tmpInspectors;
                listCompanies.Add(newCompany);
            }
            return listCompanies;
        }
    }
}
